androidmedia-playerm4vaudio-player

Play the m4v video file in background ie only sound


Im trying to play a m4v files audio alone in the background.

I tried using SoundPool and it is giving me error while loading it from the raw folder.

i used this sample program.

10-27 11:49:11.684: ERROR/AndroidRuntime(1484): Caused by: android.content.res.Resources$NotFoundException: File Hello World, AudioPlayerActivity! from drawable resource ID #0x7f040000
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     at android.content.res.Resources.openRawResourceFd(Resources.java:860)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     at   android.media.SoundPool.load(SoundPool.java:204)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     at com.sample.audio.AudioPlayerActivity.onCreate(AudioPlayerActivity.java:31)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484):     ... 11 more

But i can able to play the file using VideoView but i need only audio.

I dont know how to play the m4v file in the soundpool or some way i need to play in the background. How to achieve that?

Thanks in advance.


Solution

  • First of all take a look at supported media formats page. I've not see m4v format but if it works with you see below.

    I don't know how to use SoundPool but if you want to use it I suggest you to extrapolate a supported audio format from your video with an encoder.

    Finally I can suggest you a solution with VideoView (since you say that the video works). In your main.xml put this to refer your VideoView

    <VideoView
       android:id="@+id/video"
       android:layout_width="0.1px"
       android:layout_height="0.1px">
    </VideoView>
    

    And use this code in your onCreate method of your Activity:

    String uri = "android.resource://" + getPackageName() + "/" + R.raw.videoname;
    VideoView video = (VideoView) findViewById(R.id.video);
    video.setVideoURI(Uri.parse(uri));
    video.start();
    

    I've tried it myself and it works. I know that this is a bad way to get the audio in background but if you haven't any other solution I think that this is sufficient.