1

Recently I designed a multimedia player. But when I try to switch between landscape and portrait modes, the video restarts.

Is there any guide or tutorial for this issue?

I want video to play well in both landscape and portrait view.

This is my sample code in MainActivity.java

    VideoView videoView =(VideoView)findViewById(R.id.videoView1);  

            //Creating MediaController  
    MediaController mediaController= new MediaController(this);  
        mediaController.setAnchorView(videoView);          

          //specify the location of media file  
       Uri uri=Uri.parse(Environment.getExternalStorageDirectory() + "/video.mp4");

          //Setting MediaController and URI, then starting the videoView  
       videoView.setMediaController(mediaController);  
       videoView.setVideoURI(uri);          
       videoView.requestFocus();  
       videoView.start();  

}  
FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28
Kishor Joshi
  • 47
  • 1
  • 2
  • 5

1 Answers1

0

In android manifest set this attribute to yor video activity.

android:configChanges="orientation|keyboardHidden"
Sush
  • 3,864
  • 2
  • 17
  • 35
  • ya i did same thing and it works both for KeyborardHidden and also for screenSize thanks@Sush Now i have one more problem if i want my activity to run in background how an i do this.I mean how to use Service class to do this? – Kishor Joshi Jan 04 '14 at 04:15
  • u cannot run activity in background, service u can run. create a service class and run a looping thread inside it so t will be always running. – Sush Jan 04 '14 at 04:21
  • the service class will be created inside MainActivity or it is created as a seprate class i am reading this articlehttp://developer.android.com/guide/components/services.html – Kishor Joshi Jan 04 '14 at 04:25
  • But can you give me a link or resourcethat how to use service in my application? – Kishor Joshi Jan 04 '14 at 04:25
  • depends on ur requirement completely. But i prefere creating it out side. – Sush Jan 04 '14 at 04:26
  • u should start the service not link and running a service will make ur app is running not any activity – Sush Jan 04 '14 at 04:28
  • so i need not to any tolink my activity this it will automatically run i need to create a seprate class like http://developer.android.com/guide/components/services.html – Kishor Joshi Jan 04 '14 at 04:39
  • and inside MainActivity.java i will define it that's it will it work? – Kishor Joshi Jan 04 '14 at 04:40
  • as soon as ur activity get killed serivice will also die. simple. try out side – Sush Jan 04 '14 at 04:56
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener { private static final ACTION_PLAY = "com.example.action.PLAY"; MediaPlayer mMediaPlayer = null; public int onStartCommand(Intent intent, int flags, int startId) { ... if (intent.getAction().equals(ACTION_PLAY)) { mMediaPlayer = ... // initialize it here mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.prepareAsync(); // prepare async to not block main thread } } – Kishor Joshi Jan 05 '14 at 02:14
  • what should be value of mmediaplayer here and – Kishor Joshi Jan 05 '14 at 02:16
  • 2) mMediaPlayer.setOnPreparedListener(this); in this line showing error: – Kishor Joshi Jan 05 '14 at 02:17
  • cannot convert from void to mediaplayer – Kishor Joshi Jan 05 '14 at 02:17