2014-02-18 215 views
5

我想播放視頻,但沒有運氣,所以我正在測試一些裸露的骨骼代碼,看看問題是什麼。以下不工作,我真的不知道爲什麼:Android:如何從URL播放mp4視頻?

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.videotest" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-permission 
    android:name="android.permission.INTERNET" /> 
    <uses-permission 
     android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.videotest.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

MainActivity:

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     VideoView vid = (VideoView) findViewById(R.id.videoView1); 
     Uri vidUri = Uri.parse("http://somewebsite.com/somevideo.mp4"); 
     vid.setVideoURI(vidUri); 
     vid.setMediaController(new MediaController(this));   
     vid.start(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <VideoView 
     android:id="@+id/videoView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 

這是我的錯誤在運行代碼時給出:

02-18 10:12:44.071: E/MediaPlayer(8357): java.io.FileNotFoundException: No content provider: http://somewebsite.com/somevideo.mp4 
+0

您嘗試另一個URI的MP4視頻的開始。 –

+0

您確定該視頻存在嗎? – VinayVeluri

+0

http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view & current = 1376992942447_242.mp4 –

回答

0
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl(""http://somewebsite.com/somevideo.mp4""); 

} 

或VideoView不要忘記requestFocus的

VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview); 
myVideoView.setVideoURI(Uri.parse("http://somewebsite.com/somevideo.mp4")); 
myVideoView.setMediaController(new MediaController(this)); 
myVideoView.requestFocus(); 
myVideoView.start(); 
+0

我嘗試請求重點。沒有區別。 – spacitron

+0

該示例在模擬器上不起作用。你應該使用真正的手機 – Rollyng

12

示例代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    try { 
     setContentView(R.layout.videodisplay); 
     String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view&amp; current=1376992942447_242.mp4"; 
     VideoView videoView = (VideoView) findViewById(R.id.VideoView); 
     MediaController mediaController = new MediaController(this); 
     mediaController.setAnchorView(videoView); 
     Uri video = Uri.parse(link); 
     videoView.setMediaController(mediaController); 
     videoView.setVideoURI(video); 
     videoView.start(); 
    } catch (Exception e) { 
     // TODO: handle exception 
     Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show(); 
    } 
} 
3

我想您所呼叫vid.start()之前VideoView處於正確狀態,請嘗試以下操作:

vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      vid.start(); 
     } 
    }); 
vid.setVideoURI(videoUri); 

您還可以使用setOnErrorListener()註冊一個偵聽器,它可能會爲您提供有關失敗原因的更多信息。

0
@Override 
public void surfaceCreated(SurfaceHolder holder) { 

    Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
      + R.raw.abc); 
    try { 
     mp.setDataSource(this, video); 
     mp.prepare(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } 

    mp.setDisplay(holder); 
    mp.start(); 

或者從網址視頻只是通過您的網址進入Uri.parse()方法