teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

codeが読みやすいように修正しました

2021/10/10 06:21

投稿

oyajiinyo
oyajiinyo

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,61 +1,63 @@
1
- 画面遷移(最初の画面に戻る)と同時に音源を停止したいのですが、画面遷移はできても音源はそのまま再生されていす。だ初心者ですので、いいアドバイスをお待ちしています音源と、assets に入れています。よろしくお願いします。```swift
1
+ 2番目の画面から、画面遷移と同時に音源(assets保存)を停止したいのですが、初期画面に戻っても音源は再生されままです。画面遷移成功しています。初心者ですのでよろしくお願いします。
2
+
2
- コード
3
+ ```ここに言語を入力
3
4
  import SwiftUI
4
5
  import AVFoundation
5
6
  struct ContentView: View {
6
- var body: some View {
7
+ var body: some View {
7
- ZStack {
8
+ ZStack {
8
- NavigationView {
9
+ NavigationView {
9
- ZStack{
10
+ ZStack{
10
- Image("imgkeikan")
11
+ Image("imgkeikan")
11
- .resizable()
12
+ .resizable()
12
- .edgesIgnoringSafeArea(.all)
13
+ .edgesIgnoringSafeArea(.all)
13
- .aspectRatio(contentMode:.fill)
14
+ .aspectRatio(contentMode:.fill)
14
- VStack(spacing:100) {
15
+ VStack(spacing:100) {
15
- Text("いろいろなまつり")
16
+ Text("いろいろなまつり")
16
- .font(.system(size: 80))
17
+ .font(.system(size: 80))
17
- .foregroundColor(Color.white)
18
+ .foregroundColor(Color.white)
18
- .padding()
19
+ .padding()
19
- MaturiView()
20
+ MaturiView()
20
- .padding()
21
+ .padding()
21
- }
22
- }
23
- .navigationBarTitle(Text("はじめに"), displayMode: .inline)
24
- }
25
- .navigationViewStyle(StackNavigationViewStyle())
26
- }
27
- }
28
22
  }
23
+ }
24
+ .navigationBarTitle(Text("はじめに"), displayMode: .inline)
25
+ }
26
+ .navigationViewStyle(StackNavigationViewStyle())
27
+ }
28
+ }
29
+ }
29
30
  struct MaturiView: View {
30
31
  var body: some View{
31
32
  NavigationLink(destination: MaturiSecondView())
32
- {
33
+ {
33
- Text("おまつり")
34
+ Text("おまつり")
34
- .font(.largeTitle)
35
+ .font(.largeTitle)
35
- .foregroundColor(Color.white)
36
+ .foregroundColor(Color.white)
36
- .frame(width: 200, height: 100)
37
+ .frame(width: 200, height: 100)
37
- .background(.green) }
38
+ .background(.green) }
38
39
  }
39
40
  struct MaturiSecondView: View{
40
41
  private let maturiSound = try! AVAudioPlayer(data: NSDataAsset(name: "ymaturi")!.data)
41
42
 
42
43
  private func playSound(){
43
- maturiSound.stop()
44
+ maturiSound.stop()
44
- maturiSound.currentTime = 0.0
45
+ maturiSound.currentTime = 0.0
45
- maturiSound.play()
46
+ maturiSound.play()
46
- }
47
+ }
47
48
  var body: some View {
48
- Button(action:{
49
+ Button(action:{
49
- playSound()
50
+ playSound()
50
- }) {
51
+ }) {
51
- Image("maturi")
52
+ Image("maturi")
52
- .renderingMode(.original)
53
+ .renderingMode(.original)
53
54
  }
54
55
  }
55
56
  }
56
57
  }
57
58
  struct ContentView_Previews: PreviewProvider {
58
- static var previews: some View {
59
+ static var previews: some View {
59
- ContentView()
60
+ ContentView()
60
- }
61
+ }
61
- }
62
+ }
63
+ ```