2012-02-14 69 views
1

更新在2012年2月18日如何添加按鈕以使用MediaController播放視頻?

我收到的按鈕應該是怎樣從我的忙導師的答覆,他說,這應該是的MediaController按鍵(播放,上一個和下),我認爲它應該是總是顯示在屏幕上而不是自定義按鈕。我不認爲有播放按鈕的onclicklistener。是否有播放按鈕單擊的監聽器(除了我的代碼示例中的onCreate活動)。我試圖不必使用意圖開始另一項活動。謝謝!


我的作業項目是修改現有的項目(使用的MediaPlayer和MediaRecorder類其中拍攝有聲)加入一個按鈕(我認爲這是我必須要創造而不是當的MediaController被顯示的播放按鈕)使用MediaController單擊時播放視頻。我試圖這樣做,但我添加的代碼無法播放視頻。我的課堂教材中的例子使用MediaController的Play按鈕,所以我想學習如何讓自定義按鈕首先播放視頻。然後處理將其整合到現有項目中。請指點我現有的示例代碼或指導我進行這項工作。謝謝!


今天,我繼續創建了一個單獨的項目,它只有一個使用MediaController播放視頻的按鈕。正如所料,它仍然不起作用(無法啓動視頻,NullPointerException)。我介紹下面的項目文件。我現在無能爲力。請指出一兩件事讓我開始解決問題。再次感謝!

package com.mypackage; 

import java.io.File; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.MediaController; 
import android.widget.Toast; 
import android.widget.VideoView; 
import android.content.Context; 
import android.os.Environment; 

public class MediaActivity extends Activity { 

private String path; 
J 
private VideoView vd; 
//private Context context; 
private String TAG = " "; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //context = this; 

    Button playVideoBtn =(Button)findViewById(R.id.playVideo); 

    playVideoBtn.setOnClickListener(new OnClickListener(){ 

     public void onClick(View v){ 
      try { 
       playVideo(); 
      } catch (Exception ex) { 
       Log.e(TAG, "Failed to Start Playing the video", ex); 
      } 

       } 
    }); 
} 

private void playVideo() throws Exception { 

    vd = (VideoView) findViewById(R.id.surface_view); 

    File directoryPath = Environment 
    .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); 
    directoryPath.mkdirs(); 

    path = directoryPath.toString() + "/Familyguy_Has_Own_Orbit.3gp"; 

    if (path == "") { 
     // Tell the user to provide a media file URL/path. 
     Toast.makeText(MediaActivity.this, "Please edit MediaActivity, and set path" 
     + " variable to your media file URL/path", Toast.LENGTH_LONG).show(); 

    } else { 

     vd.setVideoPath(path); 
     vd.setMediaController(new MediaController(this));   
     vd.requestFocus(); 
     vd.start(); 
    }  
} 


} 

這裏是我的佈局文件:

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:id="@+id/playVideo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Playing Video"/> 

</LinearLayout> 

Videoview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <VideoView 
     android:id="@+id/surface_view" 
     android:layout_width="320px" 
     android:layout_height="240px" 
    /> 

</LinearLayout> 
+0

http://stackoverflow.com/questions/8681550/android-2-2-mediaplayer-is-working-fine-with-one-shoutcast-url-but-not-with-the – 2012-02-15 06:10:06

回答