Use accelerometer in my movieplayer app

删除回忆录丶 提交于 2019-12-12 02:27:34

问题


I had an app which plays videos from a web server but it just plays in landscape orientation. I want my app to use the accelerometer to play my videos both in landscape and in portrait orientation. I want my video playback feature to look like the youtube app in the iPhone. Can anyone please help me how to do this? thanks


回答1:


For this you do not need the accelerometer. Instead you listen to the notifications from the UIDevice singleton instance that are sent when the orientation changes. In your "application didFinishLaunching withOptions" method, type this:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange) name: UIDeviceOrientationDidChangeNotification object: nil];

then create this method to handle the orientation change:

- (void)deviceOrientationDidChange {
int orientation = (int)[[UIDevice currentDevice]orientation];
switch (orientation) {
    case UIDeviceOrientationFaceDown:
        NSLog(@"UIDeviceOrientationFaceDown" );
        // handle orientation
        break;
    case UIDeviceOrientationFaceUp:
        NSLog(@"UIDeviceOrientationFaceUp" );
        // handle orientation
        break;
    case UIDeviceOrientationLandscapeLeft:
        NSLog(@"UIDeviceOrientationLandscapeLeft" );
        // handle orientation
        break;
    case UIDeviceOrientationLandscapeRight:
        NSLog(@"UIDeviceOrientationLandscapeRight" );
        // handle orientation
        break;
    case UIDeviceOrientationPortrait:
        NSLog(@"UIDeviceOrientationPortrait" );
        // handle orientation
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        NSLog(@"UIDeviceOrientationPortraitUpsideDown" );
        // handle orientation
        break;
    case UIDeviceOrientationUnknown:
        NSLog(@"UIDeviceOrientationUnknown" );
        // handle orientation
        break;

    default:
        break;
}
}


来源:https://stackoverflow.com/questions/8570221/use-accelerometer-in-my-movieplayer-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!