Bonjour à tous,
Je débute sur Android et je souhaite pouvoir lire une vidéo que je viens de filmer à l'aide d'une application composée d'une seule activité. Cette activité a juste un bouton vidéo pour accéder à la caméra, et une VideoView pour voir la vidéo.
J'arrive à bien coder l'enregistrement de la vidéo, mais quand je reviens sur l'activité une fois que la vidéo a été prise et que je clique sur mon objet VideoView, l'application plante en me disant "l'application "testVideo" s'est arrêtée"
Je suppose qu'il manque quelque chose dans mon code. Dois-je utiliser un objet MediaPlayer? Ai-je oublié d'implémenter des méthodes importantes? Je vous remercie d'avance de votre aide.
Voici le code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 <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="com.example.testvideo.MainActivity$PlaceholderFragment" ****android:background="#303030"> * ****<VideoView ****android:id="@+id/videoView"* ****android:layout_width="fill_parent" ****android:layout_height="200dp" ****/> ***** ****<Button ********android:id="@+id/video" ********android:layout_width="wrap_content" ********android:layout_height="wrap_content" ********android:layout_centerHorizontal="true" ********android:layout_centerVertical="true" ********android:text="@string/video" ********android:layout_below="@id/videoView" /> * </RelativeLayout>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class MainActivity extends Activity{ Button video; Uri path; File file; * private static final int VIDEO = 0; * @Override protected void onCreate(Bundle savedInstanceState) { **super.onCreate(savedInstanceState); **setContentView(R.layout.fragment_main); **video=(Button) findViewById(R.id.video); * **video.setOnClickListener(new View.OnClickListener() { * ****@Override ****public void onClick(View arg0) { **********File file = new File(Environment.getExternalStorageDirectory()+File.separator+"my_movie.mp4");* **********Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); **********intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); **********startActivityForResult(intent, VIDEO); * ****} }); * } * @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { **if (requestCode == VIDEO) { ****if(resultCode == RESULT_OK) { ******path = data.getData(); **********VideoView video = (VideoView) findViewById(R.id.videoView); **********video.setMediaController(new MediaController(getBaseContext())); **********video.setVideoPath(path.toString()); **********video.requestFocus(); **********video.start(); ****} **} }
Partager